<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    var finalPrices = function (prices) {
      const n = prices.length;
      const ans = new Array(n).fill(0);
      for (let i = 0; i < n; ++i) {
        let discount = 0;
        for (let j = i + 1; j < n; ++j) {
          if (prices[j] <= prices[i]) {
            discount = prices[j];
            break;
          }
        }
        ans[i] = prices[i] - discount;
      }
      return ans;
    };
    console.log(finalPrices(prices = [8, 4, 6, 2, 3]))
  </script>
</body>

</html>